﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace mesoBoard.Data.Repositories
{
    public class PermissionRepository : IPermissionRepository
    {
        private mbEntities db;

        public PermissionRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public IEnumerable<ForumPermission> GetForumPermissions()
        {
            return db.ForumPermissions.AsEnumerable();
        }

        public ForumPermission GetForumPermission(int ForumPermissionID)
        {
            return db.ForumPermissions.FirstOrDefault(x => x.ForumPermissionID == ForumPermissionID);
        }

        public IEnumerable<ForumPermission> GetForumPermissions(int forumID)
        {
            return db.ForumPermissions.Where(x => x.ForumID == forumID);
        }

        public void UpdatePermission(ForumPermission tPerm)
        {
            if (tPerm.EntityKey == null)
                tPerm.EntityKey = ((ForumPermission)db.GetObjectByKey(new System.Data.EntityKey("mbEntities.ForumPermissions", "ForumPermissionID", tPerm.ForumPermissionID))).EntityKey;

            db.ForumPermissions.ApplyCurrentValues(tPerm);
            db.SaveChanges();
        }

        public bool PermissionExists(int ForumID, int RoleID)
        {
            if(db.ForumPermissions.FirstOrDefault(x => x.ForumID == ForumID && x.RoleID == RoleID) == null)
                return false;
            else return true;
        }

        public void CreateForumPermission(ForumPermission fPerm)
        {
            db.ForumPermissions.AddObject(fPerm);
            db.SaveChanges();
        }

        public void DeleteForumPermission(ForumPermission fPerm)
        {
            db.ForumPermissions.DeleteObject(fPerm);
            db.SaveChanges();
        }

    }
}
